import com.alibaba.fastjson2.util.DateUtils;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.util.StringUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class U {

    public static <T,K> boolean rangeDate(T begin,K data){
        Date endTime;
        Date beginTime;
        try{

            if(data instanceof Date){
                endTime = (Date) data;
            }else{
                endTime = DateUtils.parseDate((String) data);
            }
            if(begin instanceof Date){
                beginTime = (Date) begin;
            }else{
                beginTime = DateUtils.parseDate((String) begin);
            }
            if(beginTime.before(endTime)){
                return true;
            }
        }catch (Exception ignored){

        }
        return false;
    }
    public static <T,K> boolean rangeDateList(List<T> list, K data){
        for (T begin : list) {
            boolean checked = rangeDate(begin,data);
            if(checked){
                return true;
            }
        }
        return false;
    }

    public static <T> String dateJoin(List<T> list, String op, String cs) {
        return list.stream().map(date -> {
            if (date instanceof Date) {
                return format((Date) date, op);
            }
            return format((String) date, op);
        }).collect(Collectors.joining(cs));
    }

    public static <T> String numberJoin(List<T> list, String cs, double divide, int count) {
        return list.stream().map(data -> {
            BigDecimal num = (BigDecimal) data;
            return String.valueOf(num.divide(BigDecimal.valueOf(divide), count, RoundingMode.HALF_UP));
        }).collect(Collectors.joining(cs));
    }

    public static <T> String numberJoin(List<T> list, String cs, double divide) {
        return list.stream().map(data -> {
            BigDecimal num = (BigDecimal) data;
            return String.valueOf(num.divide(BigDecimal.valueOf(divide)));
        }).collect(Collectors.joining(cs));
    }

    public static <T> String numberJoin(List<T> list, String cs, String add, double divide) {
        return list.stream().map(data -> {
            BigDecimal num = (BigDecimal) data;
            return num.divide(BigDecimal.valueOf(divide)) + add;
        }).collect(Collectors.joining(cs));
    }

    public static <T> String numberJoin(List<T> list, String cs, String add, double divide, int count) {
        return list.stream().map(data -> {
            BigDecimal num = (BigDecimal) data;
            return num.divide(BigDecimal.valueOf(divide), count, RoundingMode.HALF_UP) + add;
        }).collect(Collectors.joining(cs));
    }

    public static <T> String join(List<T> list, String cs) {
        return StringUtil.join(list, cs);
    }

    public static <T> String join(List<T> list, String cs, String add) {
        return list.stream().map(a -> a + add).collect(Collectors.joining(cs));
    }

    public static BigDecimal sum(List<BigDecimal> list) {
        return list.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static String format(Date date, String op) {
        try {
            return DateTimeUtil.dateToLocalDateTime(date).format(DateTimeFormatter.ofPattern(op));
        } catch (Exception e) {
            return format(date.toString(), op);
        }
    }

    public static String format(String dateStr, String op) {
        Date date = DateUtils.parseDate(dateStr);
        return DateTimeUtil.dateToLocalDateTime(date).format(DateTimeFormatter.ofPattern(op));
    }

    public static String dateFormat(String date, String op1, String op2) {
        return DateTimeUtil.parseDateTime(date, DateTimeFormatter.ofPattern(op1)).format(DateTimeFormatter.ofPattern(op2));
    }
}